package com.example.demo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;

import com.example.demo.adapter.SingerAdapter;
import com.example.demo.api.ApiMusic;
import com.example.demo.entity.BaseResponse;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SingerListActivity extends AppCompatActivity {
    private SingerAdapter adapter;
    private RecyclerView recyclerView;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_singer_list);

        recyclerView = findViewById(R.id.recycler_view);
        context = this;

        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        adapter = new SingerAdapter(context);
        recyclerView.setAdapter(adapter);

        loadData();

    }

    private void loadData(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiMusic api = retrofit.create(ApiMusic.class);

        Call<BaseResponse> call = api.getArtists();

        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                BaseResponse baseResponse = response.body();
                if (baseResponse.isSuccess()){
                    adapter.setData(baseResponse.getArtists());
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

}